package com.itheima.mp.domain.query;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.vo.UserVO;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Data
@Getter
@Setter
public class PageQuery {
    private Integer currentPage = 1;
    private Integer pageSize = 5;
    private String sortedBy;
    private boolean isAsc;

    public <T> Page<T> set(OrderItem... orderItems) {
        //1.建立分页条件,排序条件
        Page<T> page = Page.of(currentPage, pageSize);
        //2建立排序条件
        if (sortedBy != null) {
            page.addOrder(new OrderItem(sortedBy,isAsc));

        }else if(orderItems!=null){
            page.addOrder(orderItems);
        }
        //3.返回
        return page;
    }

    public <T> Page<T> setWithCreateTIme() {
        return set(new OrderItem("create_time", true));
    }

    public <PO,VO> PageDTO<VO> voToDTO(Page<PO>pages,Class<VO>clazz){
        //封装成dto
        PageDTO<VO> pageDTO=new PageDTO<VO>();
        pageDTO.setPages(pages.getPages());
        pageDTO.setTotal(pages.getTotal());
        List<PO> records = pages.getRecords();
        List<VO> userVOS = BeanUtil.copyToList(records, clazz);
        pageDTO.setRecords(userVOS);
        return pageDTO;
    }


}
